package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.SalaryOvertime;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.OvertimeTaxService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;


@Results({
        @Result(name = "main", location = "salary_main.jsp"),
        @Result(name = "list", location = "overtime_tax_list.jsp"),
        @Result(name = "edit", location = "overtime_tax_add.jsp")
})
public class OvertimeTaxAction extends EHRAction {
    
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private OvertimeTaxService overtimeTaxService;
    private List<Organization> organizations;
    private LimitedList<SalaryOvertime> salaryOvertimes;

    private SalaryOvertime salaryOvertime;
    private String overtimeIds;
    
    @Action("/salary/overtime_tax_main")
    @Override
    public String execute() throws Exception {
        this.listTitle="加班费管理";
        this.listAction = "/salary/overtime_tax_list.xhtml";
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = false;
        return "main";
    }

    @Action("/salary/overtime_tax_list")
    public String list() throws Exception {
        salaryOvertimes=overtimeTaxService.query(organizationId,createLimit(),createSorter());
        this.paging(salaryOvertimes);
        this.setPageUrl("salary/overtime_tax_list.xhtml");
        return "list";
    }

    @Action("/salary/overtime_tax_edit")
    public String add() throws Exception{
        if(salaryOvertime!=null && salaryOvertime.getId()>0)
            salaryOvertime=overtimeTaxService.get(salaryOvertime.getId());
        return "edit";
    }

    @Action("/salary/overtime_tax_save")
    public String save() throws Exception{
        if (salaryOvertime == null)
            return execute();
        if(salaryOvertime!=null && salaryOvertime.getId()!=null){//modify
            SalaryOvertime tmp=overtimeTaxService.get(salaryOvertime.getId());
            BeanUtils.copyProperties(salaryOvertime,tmp,new String[]{"id","organization"});
            overtimeTaxService.save(tmp);
        }else{//add
            overtimeTaxService.save(salaryOvertime);
        }
        return execute();
    }

    @Action("/salary/overtime_tax_remove")
    public String remove() throws Exception{
        if(StringUtils.isBlank(overtimeIds)){
            return execute();
        }
        overtimeTaxService.remove(overtimeIds);
        return execute();
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public LimitedList<SalaryOvertime> getSalaryOvertimes() {
        return salaryOvertimes;
    }

    public void setSalaryOvertimes(LimitedList<SalaryOvertime> salaryOvertimes) {
        this.salaryOvertimes = salaryOvertimes;
    }

    public SalaryOvertime getSalaryOvertime() {
        return salaryOvertime;
    }

    public void setSalaryOvertime(SalaryOvertime salaryOvertime) {
        this.salaryOvertime = salaryOvertime;
    }

    public String getOvertimeIds() {
        return overtimeIds;
    }

    public void setOvertimeIds(String overtimeIds) {
        this.overtimeIds = overtimeIds;
    }
}
